<template>
  <div class="login-container">
    <div class="login-content">
      <!-- 左侧信息区 -->
      <div class="login-info">
        <div class="info-content">
          <h1 class="app-name">DBCompareX</h1>
          <p class="app-desc">专业的数据库比对与同步工具</p>
          <div class="app-features">
            <div class="feature-item">
              <el-icon class="feature-icon"><Coin /></el-icon>
              <div class="feature-text">多种数据库支持</div>
            </div>
            <div class="feature-item">
              <el-icon class="feature-icon"><Connection /></el-icon>
              <div class="feature-text">高效数据同步</div>
            </div>
            <div class="feature-item">
              <el-icon class="feature-icon"><Histogram /></el-icon>
              <div class="feature-text">数据差异比对</div>
            </div>
          </div>
        </div>
        <div class="copyright">© 2023 DBCompareX 版权所有</div>
      </div>
      
      <!-- 右侧登录区 -->
      <div class="login-box">
        <div class="login-header">
          <div class="login-title">
            <h2>欢迎登录</h2>
            <p>请使用账号密码登录系统</p>
          </div>
        </div>
        
        <el-form 
          ref="loginFormRef" 
          :model="loginForm" 
          :rules="loginRules" 
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="用户名"
              :prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              placeholder="密码" 
              :prefix-icon="Lock"
              type="password"
              size="large"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          <el-form-item class="remember-me">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button 
              :loading="loading" 
              type="primary" 
              class="login-btn"
              size="large"
              @click.prevent="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElForm } from 'element-plus';
import { User, Lock, Coin, Connection, Histogram } from '@element-plus/icons-vue';
import { useUserStore } from '@/store/user';

// 获取route和router
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

// 登录表单引用
const loginFormRef = ref<InstanceType<typeof ElForm>>();

// 加载状态
const loading = ref(false);
const rememberMe = ref(false);

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
});

// 表单验证规则
const loginRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }
  ]
});

// 清除密码
const clearPassword = () => {
  loginForm.password = '';
};

// 登录方法
const handleLogin = (event?: Event) => {
  // 确保阻止默认事件
  if (event) {
    event.preventDefault();
  }
  loginFormRef.value?.validate(async (valid) => {
    if (!valid) return;
    
    try {
      loading.value = true;
      
      // 调用登录接口
      const result = await userStore.login(loginForm);
      if (result) {
        ElMessage.success('登录成功');
        // 获取重定向的地址
        const redirectPath = route.query.redirect as string || '/';
        router.replace(redirectPath);
      } else {
        // 登录失败，只清除密码，不刷新页面
        // ElMessage.error('登录失败，请检查用户名和密码');
        clearPassword();
      }
    } catch (error) {
      console.error('登录出错:', error);
      // 发生错误时也只清除密码
      clearPassword();
    } finally {
      loading.value = false;
    }
  });
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
  
  @keyframes gradient {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
  }
  
  .login-content {
    display: flex;
    width: 900px;
    height: 550px;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.3);
    
    .login-info {
      flex: 1;
      background: linear-gradient(135deg, #071b52, #008cff);
      color: white;
      position: relative;
      padding: 40px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      
      .info-content {
        .app-name {
          font-size: 36px;
          font-weight: 700;
          margin-bottom: 16px;
          letter-spacing: 1px;
        }
        
        .app-desc {
          font-size: 16px;
          opacity: 0.8;
          margin-bottom: 60px;
        }
        
        .app-features {
          margin-top: 40px;
          
          .feature-item {
            display: flex;
            align-items: center;
            margin-bottom: 25px;
            
            .feature-icon {
              font-size: 24px;
              margin-right: 15px;
              background: rgba(255, 255, 255, 0.2);
              padding: 10px;
              border-radius: 50%;
            }
            
            .feature-text {
              font-size: 16px;
            }
          }
        }
      }
      
      .copyright {
        font-size: 12px;
        opacity: 0.6;
        align-self: center;
      }
    }
    
    .login-box {
      flex: 1;
      background: white;
      padding: 50px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      
      .login-header {
        margin-bottom: 40px;
        
        .login-title {
          h2 {
            font-size: 28px;
            color: #333;
            margin-bottom: 8px;
            font-weight: 600;
          }
          
          p {
            font-size: 16px;
            color: #999;
          }
        }
      }
      
      .login-form {
        .el-input {
          margin-bottom: 5px;
          
          :deep(.el-input__wrapper) {
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
            border-radius: 4px;
            padding: 4px 10px;
          }
        }
        
        .remember-me {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 20px;
          
          .forget-pwd {
            color: var(--primary-color);
            font-size: 14px;
            text-decoration: none;
            
            &:hover {
              text-decoration: underline;
            }
          }
        }
        
        .login-btn {
          width: 100%;
          height: 50px;
          font-size: 16px;
          font-weight: 600;
          letter-spacing: 2px;
          background: linear-gradient(to right, #4481eb, #04befe);
          border: none;
          box-shadow: 0 5px 15px rgba(68, 129, 235, 0.4);
          transition: all 0.3s ease;
          
          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(68, 129, 235, 0.6);
          }
          
          &:active {
            transform: translateY(0);
            box-shadow: 0 3px 10px rgba(68, 129, 235, 0.4);
          }
        }
      }
    }
  }
}

// 适配小屏幕
@media (max-width: 960px) {
  .login-container {
    .login-content {
      width: 90%;
      max-width: 600px;
      flex-direction: column;
      height: auto;
      
      .login-info {
        padding: 30px;
        display: none; // 在小屏上隐藏左侧
      }
      
      .login-box {
        padding: 40px 30px;
      }
    }
  }
}
</style>